package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.CheckOrderActivityToSumit;
import com.example.administrator.caigou51.EventBusClass.CheckOrderSetValue;
import com.example.administrator.caigou51.EventBusClass.GotoFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateCheckOrderActivity;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.CheckOrderBean;
import com.example.administrator.caigou51.bean.PayBean;
import com.example.administrator.caigou51.bean.PayTypeBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.OnButtonPressListener;
import com.example.administrator.caigou51.recyclerCard.card.CheckOrderCard;
import com.example.administrator.caigou51.recyclerCard.card.basic.Card;
import com.example.administrator.caigou51.recyclerCard.cardView.CheckOrderCardView;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.io.Serializable;
import java.text.DecimalFormat;

import de.greenrobot.event.EventBus;


/**
 * Created by Administrator on 2016/5/17/017.
 */
public class CheckOrderActivity extends GBaseActivity{
    MaterialListView material_listview;
    TextView textViewSumit,textViewDingDan;
    public static String TagItemIDS="TagItemIDS";
    public static String TagType="TagType";
    public static String TagNumber="TagNumber";
    public static String TagTypeBuyNow="1";
    public static String TagTypeShopCar="2";
    public static String TagTypeBugAgain="3";
    String itemids;
    String type;//下单类型
    String number;//立即订购数量
    CheckOrderBean checkOrderBean;
    CheckOrderSetValue checkOrderSetValue;
    CheckOrderCard checkOrderCard=null;
    public RelativeLayout relativeLayoutTips;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_check_order);
        findViewByIds();
        initDatas();
        EventBus.getDefault().register(this);
        TaskGetMainData(false);
    }


    protected void findViewByIds() {
        material_listview= (MaterialListView) findViewById(R.id.material_listview);
        textViewSumit = (TextView) findViewById(R.id.textViewSumit);textViewSumit.setOnClickListener(this);
        textViewDingDan= (TextView) findViewById(R.id.textViewDingDan);
        relativeLayoutTips= (RelativeLayout) findViewById(R.id.relativeLayoutTips);
    }


    protected void initDatas() {
        setTopTitle("确认订单");


        itemids=getIntent().getStringExtra(TagItemIDS);
        type=getIntent().getStringExtra(TagType);
        number=getIntent().getStringExtra(TagNumber);

    }

    protected void fillArrayInListView(CheckOrderBean checkOrderBean) {
        checkOrderCard=new CheckOrderCard(this);
        checkOrderCard.setBuyType(type);
        checkOrderCard.setRelativeLayoutTips(relativeLayoutTips);
        checkOrderCard.setTextViewSumit(textViewSumit);
        checkOrderCard.setCheckOrderBean(checkOrderBean);
        double yunfei=0;
        if (!TextUtils.isEmpty(getDefaultAddress(checkOrderBean).getShipping_fee()))
        {
            yunfei=Double.parseDouble(getDefaultAddress(checkOrderBean).getShipping_fee());
        }
        DecimalFormat    df   = new DecimalFormat("######0.00");
        double total=checkOrderBean.getPay_amount()+yunfei;

        textViewDingDan.setText(df.format(total)+"");
        material_listview.add(checkOrderCard);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewSumit:
                TaskSumitOrder(false);
            break;
        }
    }



    private void TaskGetMainData(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_SureOrder, Constant.Action.Action_SureOrder, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_SureOrder, App.getUserBean().getUserid(),
                                App.getUserBean().getSession_id(), itemids, type,number), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        checkOrderBean = JSON.parseObject(result.getData().toString(), CheckOrderBean.class);
                        fillArrayInListView(checkOrderBean);

                        if (checkOrderSetValue==null)
                        {
                            checkOrderSetValue=new CheckOrderSetValue();
                            checkOrderSetValue.setAddressEntity(getDefaultAddress(checkOrderBean));
                        }
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    private CheckOrderBean.AddressEntity getDefaultAddress(CheckOrderBean checkOrderBean) {

        for (int i=0;i<checkOrderBean.getAddress().size();i++)
        {
            if (checkOrderBean.getAddress().get(i).getIs_default()==1)
            {
                return checkOrderBean.getAddress().get(i);
            }
        }
        return null;
    }
    private void TaskSumitOrder(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.getAction_SumitOrder, Constant.Action.getAction_SumitOrder, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.getAction_SumitOrder, App.getUserBean().getUserid(),
                                App.getUserBean().getSession_id(),itemids,checkOrderCard.getCheckOrderBean().getAddress().get(0).getItemid(),type,number,checkOrderSetValue.getNote()), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {

                        PayBean payBean = JSON.parseObject(result.getData().toString(), PayBean.class);
                        Intent intetn =new Intent(CheckOrderActivity.this,PayMoneyActivity.class);
                        intetn.putExtra(PayMoneyActivity.TagPayBean, (Serializable) payBean);
                        startActivity(intetn);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    public void onEventMainThread(CheckOrderActivityToSumit checkOrderActivityToSumit) {
     TaskSumitOrder(false);
    }

    public void onEventMainThread(UpdateCheckOrderActivity updateCheckOrderActivity) {
        itemids=updateCheckOrderActivity.itemIds;
        material_listview.clear();
        textViewSumit.setOnClickListener(this);
        TaskGetMainData(false);
    }
    public void onEventMainThread(CheckOrderSetValue value) {
        if (checkOrderSetValue!=null && !TextUtils.isEmpty(value.getNote())) {
            checkOrderSetValue.setNote(value.getNote());
        }
        if (checkOrderSetValue!=null && value.getAddressEntity()!=null) {
            if (checkOrderCard!=null)
            {

                for (int i=0;i<checkOrderCard.getCheckOrderBean().getAddress().size();i++)
                {
                    if (checkOrderCard.getCheckOrderBean().getAddress().get(i).getIs_default()==1)
                    {
                        checkOrderCard.getCheckOrderBean().getAddress().remove(i);
                        value.getAddressEntity().setIs_default(1);
                        checkOrderCard.getCheckOrderBean().getAddress().add(0,value.getAddressEntity());
                        break;

                    }
                }
               // material_listview.getAdapter().notifyDataSetChanged();
                material_listview.clear();
                fillArrayInListView(checkOrderBean);
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
